package per.example.arpproject.model;


import lombok.Builder;
import per.example.arpproject.enums.StatusEnum;


import java.io.Serializable;

/**
 * API 返回结果封装
 * 泛型为data里的model数据结构
 *
 * @author Bamboo
 * @since 2020-03-18
 */
@Builder
public class ApiResult<E> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 是否成功
     */
    private Boolean success;

    /**
     * 状态码
     */
    private Integer code;

    /**
     * 提示信息
     */
    private String message;

    /**
     * 返回数据
     */
    private E data;

    private ApiResult() {}


    private ApiResult(Boolean success, Integer code, String message, E data) {
        this.success = success;
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 成功
     * @return {ApiResult}
     */
    public static ApiResult success() {
        return new ApiResult(true, StatusEnum.OK.getCode(), StatusEnum.OK.getMessage(),"");
    }

    /**
     * 成功
     * @return {ApiResult}
     */
    public static ApiResult success(String msg) {
        return new ApiResult(true, StatusEnum.OK.getCode(), msg, "");
    }

    /**
     * 成功
     * @return {ApiResult}
     */
    public static <E> ApiResult<E> success(E data) {
        return new ApiResult<>(true, StatusEnum.OK.getCode(), StatusEnum.OK.getMessage(), data);
    }

    /**
     * 成功
     * @return {ApiResult}
     */
    public static <E> ApiResult<E> success(String msg, E data) {
        return new ApiResult<>(true, StatusEnum.OK.getCode(), msg, data);
    }

    /**
     * 错误
     * @return {ApiResult}
     */
    public static ApiResult error(String msg) {
        return new ApiResult(false, StatusEnum.INTERNAL_SERVER_ERROR.getCode(), msg, "");
    }

    /**
     * 错误
     * @return {ApiResult}
     */
    public static ApiResult error(Integer code, String msg) {
        return new ApiResult(false, code, msg, "");
    }

    /**
     * 错误
     * @param statusEnum 错误码
     * @return
     */
    public static ApiResult error(StatusEnum statusEnum) {
        return new ApiResult(false, statusEnum.getCode(), statusEnum.getMessage(), "");
    }

    /**
     * 错误
     * @return {ApiResult}
     */
    public static <E> ApiResult<E> error(Integer code, String msg, E data) {
        return new ApiResult<>(false, code, msg, data);
    }

    /**
     * 自定义提示
     * @return {ApiResult}
     */
    public static <E> ApiResult<E> msg(Boolean success, Integer code, String msg, E data) {
        return new ApiResult<>(success, code, msg, data);
    }

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public E getData() {
        return data;
    }

    public void setData(E data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "ApiResult{" +
                "success=" + success +
                ", code=" + code +
                ", message='" + message + '\'' +
                '}';
    }
}
